﻿using System.IO;
using System;
using System.Collections.Generic;

using System.Text;

namespace xReplace
{
    class PrintCommand:AbstractProcessingCommand
    {
        public PrintCommand(ref Content pipes)
            : base(ref pipes)
        { }

        public override bool ProcessCommand(System.Xml.XmlNodeList param)
        {
            string label = this.GetNodeValue("fromlabel", param);
            string filename = this.GetNodeValue("filename", param);
            string message = this.GetNodeValue("message",param);
            string tmp = (label != "") ? this.contentPipelines.LoadContent(label) : message;
            string renameRules = this.GetNodeValue("renamerules", param);
            if (GetNodeAttributeAsBoolean("message", "escape", param))
                message= SpecialCharHelper.Unescape(message);

            if ((filename != "") && (this.NodeExists("filename", param)))
            {
                if (renameRules != "")
                    filename = RenameHelper.PerformRename(filename, renameRules, this.contentPipelines);
                File.AppendAllText(filename,tmp);
            }

            Console.WriteLine(tmp);
            Console.Out.Flush();
            return true;
        }
    }
}
